<template>
  <el-dialog title="外协计划产品选择"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             @close="cancelSelect"
             center
  >
    <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table  border ref="productTable" v-loading="loading" :data="planProductList"   @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="计划编号" align="center" prop="planNo" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="产品编号" align="center" prop="productCode" >
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column v-if="outsourceType=='G'" label="工序编号" align="center" prop="processCode" />
      <el-table-column v-if="outsourceType=='G'" label="工序名称" align="center" prop="processName" />
      <el-table-column v-if="outsourceType=='G'" label="是否质检" align="center" prop="needCheck" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.needCheck"/>
        </template>
      </el-table-column>
      <el-table-column label="需求数量" align="center" prop="productQuantity" />
<!--      <el-table-column label="需求截止日期" align="center" prop="deadline" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
    </el-table>


  </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancelSelect">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Template from "@/views/print/printtemplate/list.vue";
import BomSelectSingle from "@/components/mdBomSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import PlanLine from "@/views/mes/pro/outsourcingplan/planline.vue";
import { getPlanProductList} from "@/api/mes/pro/outsourcingplan";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "PlanProductSelector",
  components: {Template, BomSelectSingle, ItemSelect, PlanLine},
  dicts: ['erp_payment_type','sys_yes_no'],
  props: {
    outsourceType: {
      type: String,
      default: 'Z'
    },
    selectedList:{
      type: Array,
      default: () => []
    }
    ,showDialog:{
      type: Boolean,
      default: false
    },
    needAddProductList:{
      type:Map,
      default: () => new Map()
    }
  },
  data() {
    return {
      columnsConfig:[],
      showFlag: this.showDialog,
      selectedRows:[],
      oldRow: {},
      lineList: [],
      planProductList:[],
      selectedPlanNo:null,
      autoGenFlag: false,
      optType: 'view',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        outsourceType: this.outsourceType,
        planNo: null, productCode: null,
        productName:null,

      },
      // 表单参数


    };
  },
  created() {
    this.getList();
    this.getItemBase()
  },
  watch: {

  },
  methods: {
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询外协计划产品列表 */

    getList() {
      this.loading = true;
      getPlanProductList(this.queryParams).then(response => {
          this.planProductList = response.data;
          this.loading = false;
          this.$nextTick(function (){
            let result=this.planProductList;
            if(this.needAddProductList.size>0){

              this.needAddProductList.forEach((value, key) => {
                if(!result.find(item=>item.planlineId==key)){
                  result.push(value);
                }
              });
            }
            if(this.planProductList&&this.planProductList.length>0&&this.selectedList.length>0){
               result = this.planProductList.filter(itemA => !this.selectedList.find(itemB => itemB.planlineId === itemA.planlineId));
            }
            this.planProductList = result;
          })
        }
      );
    },
    cancelSelect(){
    this.$emit('cancelSelectProduct')
    },

    handleSelectionChange(val){
      this.selectedRows = val;
    },
    //确定选中
    confirmSelect(){
      if(this.selectedRows==null || this.selectedRows.length==0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.cancelSelect();
    },

  }
}
</script>
